package com.example.kingdee.sqlTool.common;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.kingdee.sqlTool.sqltoolEnum.FormIdEnum;

import java.util.*;

public class DataObject extends SwMap {

    public DataObject(String tableName) {
        setFormId(tableName);
    }

    public DataObject(FormIdEnum formIdEnum) {
        String formId = FormIdEnum.getDBTableName(formIdEnum.name());
        setFormId(formId);
//        setIsControlPrecision(true);
    }

    public DataObject() {
    }

    /**
     * 指定业务对象表单Id
     * @param formId
     */
    public void setFormId(String formId){
        put("formid",formId);
    }
    /**
     * 指定业务对象表单Id
     * @param bool
     */
    public void setIsControlPrecision(boolean bool){
        put("IsControlPrecision",bool);
    }
    /**
     * 指定业务对象表单Id
     * @param bool
     */
    public void setIsEntryBatchFill(boolean bool){
        put("IsEntryBatchFill",bool);
    }
    /**
     * 指定业务对象表单Id
     * @param bool
     */
    public void setIsAutoAdjustField(boolean bool){
        put("IsAutoAdjustField",bool);
    }
    /**
     * 是否自动审核
     * @param bool
     */
    public void setIsAutoSubmitAndAudit(boolean bool){
        put("IsAutoSubmitAndAudit",bool);
    }
    /**
     * 指定业务对象表单Id
     * @param bool
     */
    public void setIsDeleteEntry(boolean bool){
        put("IsDeleteEntry",bool);
    }
    public String getFormId(){
        return String.valueOf(get("formid"));
    }

    /**
     * 指定要修改的字段
     */
    public DataObject addNeedUpDateFields(String... needUpDateFields){
        ArrayList<String> needUpDateFieldList = Optional.ofNullable(getNeedUpDateFields()).orElse(new ArrayList<>());
        needUpDateFieldList.addAll(Arrays.asList(needUpDateFields));
        setNeedUpDateFields(needUpDateFieldList);
        return this;
    }

    public void setNeedUpDateFields(ArrayList<String> needUpDateFields){
        put("NeedUpDateFields",needUpDateFields);
    }

    public ArrayList<String> getNeedUpDateFields() {
        return Optional.ofNullable((ArrayList<String>)get("NeedUpDateFields")).orElse(new ArrayList<>());
    }

    /**
     * 单据编码集合，数组类型，格式：[No1,No2,...]（使用编码时必录）
     */
    public DataObject addNumbers(String... numbers){
        ArrayList<String> numberList = Optional.ofNullable(getNumbers()).orElse(new ArrayList<>());
        numberList.addAll(Arrays.asList(numbers));
        setNumbers(numberList);
        return this;
    }

    public void setNumbers(ArrayList<String> numberList){
        put("Numbers",numberList);
    }

    /**
     * 交互标志集合，字符串类型，分号分隔，格式："flag1;flag2;..."（非必录） 例如（允许负库存标识：STK_InvCheckResult）
     */
    public DataObject addInterFlags(String... numbers){
        ArrayList<String> numberList = Optional.ofNullable(getNumbers()).orElse(new ArrayList<>());
        numberList.addAll(Arrays.asList(numbers));
        setInterFlags(numberList);
        return this;
    }

    public void setInterFlags(ArrayList<String> numberList){
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < numberList.size(); i++) {
            if (i == numberList.size() - 1){
                buffer.append(numberList.get(i));
            }else {
                buffer.append(numberList.get(i)).append(";");
            }
        }
        put("InterationFlags",buffer.toString());
    }

    public ArrayList<String> getNumbers() {
        return Optional.ofNullable((ArrayList<String>)get("Numbers")).orElse(new ArrayList<>());
    }

    /**
     * 根据ids删除
     */
    public DataObject addIds(String... idArr){
        String ids = getIds();
        StringBuffer idBuffer = new StringBuffer();
        for (int i = 0; i < idArr.length; i++) {
            if (i != 0){
                idBuffer.append(",");
            }
            idBuffer.append(idArr[i]);
        }
        if (StringUtils.isEmpty(ids)){
            ids = idBuffer.toString();
        }else {
            ids = ids + "," + idBuffer;
        }
        setIds(ids);
        return this;
    }

    public void setIds(String ids){
        put("Ids",ids);
    }

    public String getIds() {
        if (get("Ids") == null){
            return "";
        }else {
            return (String) get("Ids");
        }
    }

    /**
     *添加表单数据包
     * @param key
     * @param value
     */
    public void setAttribute(String key,Object value){
        Map<String,Object> data = getData();
        data.put(key,value);
        setData(data);
    }
    public Object getAttribute(String key){
        return getData().get(key);
    }

    public HashMap<String,Object> getData() {
        return Optional.ofNullable((HashMap<String,Object>)get("Model")).orElse(new LinkedHashMap<>());
    }
    public void setData(Map<String,Object> data) {
        put("Model",data);
    }

}
